package com.zlm.common.base;

import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.dto.CoordinatesDto;
import com.zlm.common.dto.baidurequest.BaiduResultDto;
import com.zlm.common.util.BaiduMapUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 * 基础SERVICE的简单实现
 *
 * @Author: duyufu
 * @Date: 2019/3/14 14:50
 */
public abstract class AbstractBaseService<Dao extends BaseDao<Entity>, Entity extends Serializable> implements BaseService<Entity> {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    protected Dao baseDao;

    /**
     * 需要在子类中注入
     *
     * @param baseDao
     */
    public abstract void setBaseDao(Dao baseDao);

    @Override
    public int getCount() {
        return this.baseDao.selectCount();
    }

    @Override
    public int getCount(Entity entity) {
        return this.baseDao.selectCountByEntity(entity);
    }

    @Override
    public Entity getEntity(Entity entity) {
        return this.baseDao.selectEntity(entity);
    }

    @Override
    public List<Entity> getAll() {
        return this.baseDao.selectAll();
    }

    @Override
    public List<Entity> getList(Entity entity, RowBounds rowBounds) {
        return this.baseDao.selectListByEntity(entity, rowBounds);
    }

    @Override
    public List<Entity> getList(Entity entity) {
        return this.baseDao.selectListByEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Entity entity) {
        return this.baseDao.insert(entity) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modify(Entity entity) {
        return this.baseDao.update(entity) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Entity entity) {
        return this.baseDao.delete(entity) >= 0;
    }

    @Override
    public PageEntity getPage(Integer pageNumber, Integer pageSize, Integer count, List<?> list){
        PageEntity page = new PageEntity();
        pageNumber = (pageNumber == null || pageNumber.intValue() == NumberConstants.NUM_0 ? CommonConstants.pageNumber :
                pageSize);
        pageSize = (pageSize == null ? CommonConstants.pageSize : pageSize);
        int countPage = count / pageSize;
        if (count % pageSize != 0) {
            countPage++;
        }
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        page.setRows(list);
        page.setRowSize(count);
        page.setCountPage(countPage);
        return page;
    }

    @Override
    public PageEntity getPage(PageEntity page, Integer count, List<?> list){
        Integer pageSize = page.getPageSize();
        Integer pageNumber = page.getPageNumber();
        int countPage = count / pageSize;
        if (count % pageSize != 0) {
            countPage++;
        }
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        page.setRows(list);
        page.setRowSize(count);
        page.setCountPage(countPage);
        return page;
    }


    public PageEntity setPage(Integer pageNumber, Integer pageSize) {
        PageEntity page = new PageEntity();
        if (null == pageSize || pageSize >= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }
        if (null == pageNumber || pageNumber.intValue() == NumberConstants.NUM_0) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (pageNumber-1) * pageSize;
        }
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        return page;
    }

    public String getCity(CoordinatesDto coordinates) {
        BaiduResultDto baiduResult = BaiduMapUtils.getGeocoder(coordinates);
        String city = null;
        if (baiduResult.getStatus() == 0 && baiduResult.getResult() != null && baiduResult.getResult().getAddressComponent() != null) {
            city = baiduResult.getResult().getAddressComponent().getCity();
        }
        return city;
    }

}
